package com.hk.commons.locks;

import lombok.RequiredArgsConstructor;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @author Kevin
 */
@RequiredArgsConstructor
public class AutoCloseLock implements AutoCloseable {

    private final Lock lock;

    @Override
    public void close() {
        this.unlock();
    }

    public void lock() {
        lock.lock();
    }

    public void lockInterruptibly() throws InterruptedException {
        lock.lockInterruptibly();
    }

    public boolean tryLock() {
        return lock.tryLock();
    }

    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return lock.tryLock(time, unit);
    }

    public void unlock() {
        lock.unlock();
    }

    public Condition newCondition() {
        return lock.newCondition();
    }
}
